import 'package:easydouban/bean/i_search_entity.dart';
import 'package:easydouban/provider/search_provider.dart';
import 'package:easydouban/sql/dao/search_dao.dart';
import 'package:easydouban/tools/toastTool.dart';
import 'package:flutter/cupertino.dart';
import 'package:provider/provider.dart';

///点击搜索按钮，将此搜索记录添加到数据库
exeSearchInsert(SearchRecord entity,BuildContext context) async {
  bool isExist = await SearchDao.getInstance().isExist(entity.searchKey);
  if(!isExist){
    final provider = context.read<SearchProvider>();
    provider.addSingleBean = entity;
    await SearchDao.getInstance().insert(entity);
  }
}

///清空搜索记录
exeSearchClear(BuildContext context) async{
  final provider = context.read<SearchProvider>();
  if(provider.searchList.isEmpty){
    showFailedToast('记录为空，无需清空!');
    return;
  }

  int count = await SearchDao.getInstance().deleteAll();
  if(count > 0){
    provider.clear();
    showSuccessToast('清空完成!');
  }else{
    showFailedToast('清空失败!');
  }

}

isSearchInserted(String key) async => await SearchDao.getInstance().isExist(key);
